package com.smart_campus.campus.controller;

import com.smart_campus.campus.domain.CampusMedia;
import com.smart_campus.campus.domain.CapumsNotice;
import com.smart_campus.campus.service.CapumsNoticeService;
import com.smart_campus.campus.vo.CampusNoticeVo;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.service.DeptService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Alfred
 * @time: 2020/6/28 19:27
 */
@RestController
@RequestMapping("notices")
public class CampusNoticeController {

    @Autowired
    private CapumsNoticeService campusNoticeService;

    @Autowired
    private DeptService deptService;

    /**
     * 查询学校公告
     * @return campusNoticeVo
     */
    @RequestMapping("queryNotice")
    public Object queryNotice(CampusNoticeVo campusNoticeVo){
        System.out.println("campusNoticeVo = " + campusNoticeVo);
        return this.campusNoticeService.queryNotice(campusNoticeVo);
    }

    /**
     * 添加学校公告
     * @param campusNotice
     * @return
     */
    @PostMapping("addCampusNotice")
    public ResultObj addCampusNotice(CapumsNotice campusNotice){
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            campusNotice.setUserId(user.getUserId());
            campusNotice.setCreateTime(new Date());
            Integer integer = this.campusNoticeService.queryMax();
            campusNotice.setOrdernum(integer+1);
            this.campusNoticeService.insertSelective(campusNotice);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @RequestMapping("deleteCampusNotice")
    public ResultObj deleteCampusNotice(Integer id){
        try {
            this.campusNoticeService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 批量删除公告
     * @param ids
     * @return
     */
    @RequestMapping("deleteBatchCampusNotice")
    public ResultObj deleteBatchCampusNotice(Integer ids[]){
        try {
            for (Integer id: ids) {
                this.campusNoticeService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改公告
     * @param capumsNotice
     * @return
     */
    @RequestMapping("updateCampusNotice")
    public ResultObj updateCampusNotice(CapumsNotice capumsNotice){
        try {
           this.campusNoticeService.updateByPrimaryKeySelective(capumsNotice);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询单个
     * @param capumsNotice
     * @return
     */
    @RequestMapping("selectnoticesdBody")
    public CapumsNotice selectJournalism(CapumsNotice capumsNotice){
        try {
            capumsNotice=this.campusNoticeService.selectByPrimaryKey(capumsNotice.getId());
            return capumsNotice;
        } catch (Exception e) {
            e.printStackTrace();
            return capumsNotice;
        }
    }


    @GetMapping("getAllNotice")
    public Object getAllNotice(){
        return this.campusNoticeService.getAllNotice();
    }

    @GetMapping("getAllDept")
    public Object getAllDept(){
       return this.deptService.getAllDept();
    }



}
